package com.yc.goods.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yc.entity.Goodsinfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface GoodsInfoDao extends BaseMapper<Goodsinfo> {

    /**
     * 通过商品名进行查询
     *
     * @param gname
     * @return
     */
    @Select("SELECT * FROM goodsinfo WHERE gname LIKE CONCAT('%', #{gname}, '%')")
    List<Goodsinfo> findByGname(@Param("gname") String gname);


    /**
     * 通过商品编号集合进行查询
     *
     * @param gnos
     * @return
     */
    @Select({
            "<script>",
            "SELECT * FROM goodsinfo WHERE gno IN",
            "<foreach item='item' collection='gnos' separator=',' open='(' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    List<Goodsinfo> findByGno(List<Integer> gnos);

    /**
     * 页面显示前四条数据
     * @return
     */
    @Select("select * from goodsinfo gf1 where 4>\n" +
            " (select count(gno) from goodsinfo gf2 where gf1.tno = gf2.tno > gf2.gno) \n" +
            " order by gf1.tno asc, gf1.gno desc")
    List<Goodsinfo> finds();

}
